<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

class TemplateController extends BaseController {

    /**
     * 模板列表
     */
    public function templateList() {
        $t = I('t', 'pc'); // pc or  mobile        
        $tpl = ($t == 'pc') ? 'Home' : 'Mobile';
        $arr = scandir("./Tpl/$tpl/");
        foreach ($arr as $key => $val) {
            if ($val == '.' || $val == '..')
                continue;
            $template_config[$val] = include "./Tpl/$tpl/$val/config.php";
        }
        $this->assign('t', $tpl);
        // $default_theme =  xrkCache("hidden.{$t}_default_theme"); // //$default_theme = M('Config')->where("name='{$t}_default_theme'")->getField('value');
        $template_arr = include("./Application/$tpl/Conf/html.php");
        $this->assign('default_theme', $template_arr['DEFAULT_THEME']);
        $this->assign('template_config', $template_config);
        $this->display();
    }

    /**
     * 模板更改
     */
    public function changeTemplate() {
        $t = I('t', 'Home'); // Home or  Mobile     
        $key = I('key');
        $m = ($t == 'Home') ? 'Home' : 'Mobile';
        //$default_theme = xrkCache("hidden.{$t}_default_theme"); // 获取原来的配置                
        //xrkCache("hidden.{$t}_default_theme",$_GET['key']);
        //xrkCache('hidden',array("{$t}_default_theme"=>$_GET['key']));                         
        // 修改文件配置  
        if (!is_writeable("./Application/$m/Conf/html.php"))
            return "文件/Application/$m/Conf/html.php不可写,不能启用模板,请修改权限!!!";
        $module = strtoupper($m);
      
        $config_html = "<?php
    return array(
            'VIEW_PATH'       =>'./Tpl/$m/',
            'DEFAULT_THEME'    =>'{$key}', 
            'TMPL_PARSE_STRING'  =>array(
                            '__{$module}__'     => '/Tpl/$m/{$key}', 
               ),
    );
?>";
//                              echo htmlentities( file_get_contents("./Application/$m/Conf/config.php"));die();
        file_put_contents("./Application/$m/Conf/html.php", $config_html);
        $this->success("操作成功!!!", U('Admin/Template/templateList', array('t' => $t)));
    }

}
